import request from '@/utils/request'

/**
 * 查询商品类别列表
 * @param {object} query - 查询参数
 * @returns {Promise}
 */
export function listCategories(query) {
  return request({
    url: '/api/v1/categories/list',
    method: 'get',
    params: query
  })
}

/**
 * 获取商品类别详情
 * @param {number} id - 类别ID
 * @returns {Promise}
 */
export function getCategory(id) {
  return request({
    url: `/api/v1/categories/${id}`,
    method: 'get'
  })
}

/**
 * 新增商品类别
 * @param {object} data - 类别数据
 * @returns {Promise}
 */
export function addCategory(data) {
  return request({
    url: '/api/v1/categories/save',
    method: 'post',
    data: data
  })
}

/**
 * 修改商品类别
 * @param {object} data - 类别数据
 * @returns {Promise}
 */
export function updateCategory(data) {
  return request({
    url: '/api/v1/categories/save',
    method: 'post',
    data: data
  })
}

/**
 * 删除商品类别
 * @param {number|Array} ids - 类别ID或ID数组
 * @returns {Promise}
 */
export function delCategory(ids) {
  return request({
    url: '/api/v1/categories/delete',
    method: 'delete',
    data: Array.isArray(ids) ? { ids } : { ids: [ids] }
  })
}

/**
 * 绑定商品到类别
 * @param {number} categoryId - 类别ID
 * @param {number} productId - 商品ID
 * @returns {Promise}
 */
export function bindProductToCategory(categoryId, productId) {
  return request({
    url: `/api/v1/categories/bind/${categoryId}/${productId}`,
    method: 'post'
  })
}

/**
 * 从类别中解绑商品
 * @param {number} categoryId - 类别ID
 * @param {number} productId - 商品ID
 * @returns {Promise}
 */
export function unbindProductFromCategory(categoryId, productId) {
  return request({
    url: `/api/v1/categories/unbind/${categoryId}/${productId}`,
    method: 'delete'
  })
}
